package com.hzj.controller;


import com.hzj.controller.utils.IPUtils;
import com.hzj.controller.utils.RedisUtil;
import com.hzj.pojo.Patient;
import com.hzj.pojo.UserInfo;
import com.hzj.service.PatientService;
import com.hzj.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 * 就诊人表 前端控制器
 * </p>
 *
 * @author hzj
 * @since 2021-05-08
 */
@RestController
@RequestMapping("/patient")
public class PatientController {

    @Autowired
    PatientService ps;
    @Autowired
    RedisUtil redisUtil;

    @PostMapping("/auth/get/{id}")
    public Patient getById(@PathVariable Integer id) {
        return ps.getById(id);
    }

    @PostMapping("/authve")
    public Result save(@RequestBody Patient patient, HttpServletRequest request) {
        UserInfo userInfo = (UserInfo) redisUtil.get("userInfo"+ IPUtils.getIpAddr(request));
        patient.setUserId(userInfo.getId());
        boolean save = ps.save(patient);
        if (!save) {
            return Result.fail();
        }
        return Result.ok();
    }

    @PostMapping("/auth/update")
    public Result updateById(@RequestBody Patient patient) {
        //修改时间改为当前时间
        patient.setUpdateTime(new Date());
        boolean update = ps.updateById(patient);
        if (!update) {
            return Result.fail();
        }
        return Result.ok();
    }

    @PostMapping("/auth/remove/{id}")
    public Result removeById(@PathVariable Integer id) {
        boolean del = ps.removeById(id);
        if (!del) {
            return Result.fail();
        }
        return Result.ok();
    }
}

